<?php
apf_require_class('Oa_Job_Attendance_Abstract');
class Oa_Job_Attendance_ExportReport extends Oa_Job_Attendance_Abstract {

    /** 记录保存位置 **/
    private $export_report = "/home/sogo/report_2012_07.csv";

    /** 考勤系统读 **/
    private $readdb = "attendance_slave";

    public function run(){
        $this->log("开始导出");
        $title = array('用户名','code','开始时间','结束时间','申请时间','请假类型','状态','请假时长');
        $this->put_content_csv($title, $this->export_report);
        $record_list = $this->record_list();
        foreach($record_list as $user_id=>$r){
            foreach($r as $a){
                $this->put_content_csv($a, $this->export_report);
            }
        }
        $this->log("导出完成");
    }

    /**
     * 请假信息组装
     */
    public function record_list(){
        $result = array();
        $leave_records = $this->get_records();
        $user_info = $this->get_users_info($leave_records['ids']);
        foreach($leave_records['records'] as $user_id=>$record){
            foreach($record as $rec){
                $result[$user_id][] = array(
                        'name'=>$user_info[$user_id]['name'],//用户名
                        'code'=>$user_info[$user_id]['code'],//code
                        'start_time'=>$rec['start_time'],//开始时间
                        'end_time'=>$rec['end_time'],//结束时间
                        'apply_time'=>$rec['apply_time'],//申请时间
                        'type'=>$rec['type'],//类型
                        'status'=>$rec['status'],//状态
                        '$leave_time'=>$rec['leave_time']//请假时长
                        );
            }
        }
        return $result;
    }

    /**
     * 取用户请假记录
     */
    private function get_records(){
        $offset = 0;
        $limit = 100;
        $ids = array();
        $records = array();
        while(true){
            $sql = "SELECT * FROM leave_record WHERE start_time >='2012-07-01' and end_time <='2012-07-28 23:00:00' limit $offset,$limit";
            $rows = $this->get_db_rows($sql, $this->readdb);
            foreach($rows as $row){
                $records[$row['user_id']][] = $row;
                if(!isset($ids['user_id'])){
                    $ids[$row['user_id']] = $row['user_id'];
                }
            }
            if(empty($rows)){
                break;
            }
            $offset = $offset + $limit;
        }
        return array('records'=>$records,'ids'=>$ids);
    }

    /**
     * 获取请假用户信息
     */
    private  function get_users_info($ids){
        $result = array();
        if(empty($ids)){
            return $result;
        }
        $ids = implode(",", $ids);
        $sql = "SELECT * FROM user_info where user_id in($ids)";
        $rows = $this->get_db_rows($sql, $this->readdb);
        foreach($rows as $row){
            $result[$row['user_id']] = $row;
        }
        return $result;
    }

    /**
     * 文件写入
     */
    private function put_content_csv($data,$file) {
        $str = trim(implode(",", $data))."\n";
        $handle = fopen($file, 'a+');
        fwrite($handle, $str);
    }

}